package com.yidu.ladder.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.yidu.ladder.system.domain.User;

/**
 * 访问拦截器
 * @author ld
 *
 */
public class VisitInterceptor extends HandlerInterceptorAdapter {
	
	/**
	 * 进入拦截器后首先进入的方法
	   * 返回false则不再继续执行
	   * 返回true则继续执行
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, 
			Object handler) throws Exception {
		//判断用户是否登录
		User loginUser = (User) request.getSession().getAttribute("user");
		System.err.println("拦截到了，user："+loginUser);
		if(loginUser == null){
			response.sendRedirect("/bestinc/login.html");
			return false;
	 	}
		return true;
	}
	
	/**
           * 生成视图时执行，可以用来处理异常，并记录在日志中
     */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, 
			Object handler, Exception ex) throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}
	
	/**
           * 生成视图之前执行，可以修改ModelAndView
     */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, 
			Object handler, ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}
}
